// src/stores/feedback.ts
import { defineStore } from 'pinia';
import axios from '@/plugins/axios';

export interface Feedback {
  id: number;
  content: string;
  submitterId: number | null;
  submitterName: string;
  createTime: string;
  reply: string | null;
  replierId: number | null;
  replierName: string | null;
  replyTime: string | null;
}

export interface FeedbackCreatePayload {
  content: string;
  submitterId: number;
}

export interface FeedbackReplyPayload {
  id: number;
  reply: string;
  replierId: number;
}

export const feedbackFields = [
  {
    key: 'content',
    label: '反馈内容',
    type: 'textarea',
    placeholder: '请输入反馈内容',
    maxLength: 1000,
  },
];

export const replyFields = [
  {
    key: 'reply',
    label: '回复内容',
    type: 'textarea',
    placeholder: '请输入回复内容',
    maxLength: 1000,
  },
];


export const useFeedbackStore = defineStore('feedback', {
  state: () => ({
    feedbacks: [] as Feedback[],
    loading: false,
  }),
  actions: {
    async fetchFeedbacks() {
      this.loading = true;
      try {
        const response = await axios.get('/feedbacks');
        if (response.data.code === 200 && Array.isArray(response.data.data)) {
          this.feedbacks = response.data.data.map((fb: any) => ({
            ...fb,
            createTime: fb.createTime ? new Date(fb.createTime).toLocaleString() : 'N/A',
            replyTime: fb.replyTime ? new Date(fb.replyTime).toLocaleString() : null,
          }));
          return this.feedbacks;
        }
        throw new Error(response.data.message || '获取反馈记录失败');
      } catch (error: any) {
        console.error('获取反馈记录失败:', error);
        const message = error.response?.data?.message || '获取反馈记录时发生网络错误';
        throw new Error(message);
      } finally {
        this.loading = false;
      }
    },

    async createFeedback(payload: FeedbackCreatePayload): Promise<Feedback> {
      try {
        const response = await axios.post('/feedbacks', payload);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '创建反馈失败');
      } catch (error: any) {
        console.error('创建反馈失败:', error);
        const message = error.response?.data?.message || '创建反馈时发生网络错误';
        throw new Error(message);
      }
    },

    async replyToFeedback(payload: FeedbackReplyPayload): Promise<Feedback> {
      try {
        const response = await axios.put('/feedbacks/reply', payload);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '回复反馈失败');
      } catch (error: any) {
        console.error('回复反馈失败:', error);
        const message = error.response?.data?.message || '回复反馈时发生网络错误';
        throw new Error(message);
      }
    },

    async deleteFeedback(id: number): Promise<void> {
      try {
        const response = await axios.delete(`/feedbacks/${id}`);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '删除反馈失败');
      } catch (error: any) {
        console.error('删除反馈失败:', error);
        const message = error.response?.data?.message || '删除反馈时发生网络错误';
        throw new Error(message);
      }
    },
  },
});